#include <jni.h>
#include <string>

#define LOG_TAG "System.out"

#include<android/log.h>

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_stringFromJNI(JNIEnv *env, jclass type) {

    //1 静态函数
    jclass j_class = env->FindClass("com/dingqb/ndkproject/nativelib/ww");
    //2
    jmethodID j_methodID = env->GetStaticMethodID(j_class, "sayHello", "(Ljava/lang/String;)V");// 参数说明 (Ljava/lang/String;)是签名，V是返回值
    //3
    const char *chara = "call from jni data -调用java层静态函数,this data comes from jni String ";
    jstring data = env->NewStringUTF(chara);
    //4 执行java 静态方法
    env->CallStaticVoidMethod(j_class, j_methodID, data);
    //5 释放资源
    env->DeleteLocalRef(j_class);
    env->DeleteLocalRef(data);
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_getName(JNIEnv *env, jclass type) {

    std::string hello = "Hello from C++";
    jstring s = (*env).NewStringUTF("（*env）.NewStringUTF(1231321) 调用调用方法");
    jstring s2 = env->NewStringUTF("env->NewStringUTF(1231321) 调用调用方法");
    const char *chara = env->GetStringUTFChars(s, 0);
    const char *chara2 = env->GetStringUTFChars(s2, 0);
    LOGE("dingqb%s", chara);
    LOGE("dingqb$s", chara2);
    LOGE("dingqb%s", "Java_com_dingqb_ndkproject_nativelib_ww_stringFromJNI()在Jni层被调用");
    Java_com_dingqb_ndkproject_nativelib_ww_stringFromJNI(env, type);
    LOGE("dingqb%s", "Java_com_dingqb_ndkproject_nativelib_ww_stringFromJNI--Jni 层调用Jni方法");
    return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_var(JNIEnv *env, jclass type) {
// 类成员函数
    //1 找到该类
    jclass j_class = env->FindClass("com/dingqb/ndkproject/nativelib/ww");
    //2 找到对应的方法
    jmethodID j_methodID = env->GetMethodID(j_class, "sayClassMethod", "(Ljava/lang/String;)V");
    //3 java构造函数方法
    jmethodID j_construct = env->GetMethodID(j_class, "<init>", "()V");
    //4 生成Object
    jobject j_obj = env->NewObject(j_class, j_construct, NULL);
    jstring data = env->NewStringUTF("call from jni data-调用java层类函数");
    //5 调用java类中成员方法
    env->CallVoidMethod(j_obj, j_methodID, data);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_showPersonName(JNIEnv *env, jclass type) {
    // 第一步 获取class
    jclass j_class = env->FindClass("com/dingqb/ndkproject/nativelib/Jni");
    // 第二步 获取方法，其中方法有参数列表、返回值。具体情况参考https://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html
    // 1、静态方法、类方法
    jmethodID j_methodID = env->GetStaticMethodID(j_class, "getPersonName", "()V");
    // 第三步 执行java静态方法。
    env->CallStaticVoidMethod(j_class, j_methodID);
    std::string hello = "这是一个通过JNI 调用 java方法的过程";
    for (int i = 0; i < 10; ++i) {
        LOGE("dingqb%d", i);
    }
    return env->NewStringUTF(hello.c_str());
}